<?
if(! defined('BASEPATH') ){ exit('Unable to view file.'); }
$n1 = rand(1000, 9999);
$n2 = rand(1000, 9999);
$n3 = rand(1000, 9999);
$n4 = rand(1000, 9999);
$code = $n1."-".$n2."-".$n3."-".$n4;

if(isset($_POST['add'])){
	$code = $db->EscapeString($_POST['code']);
	$coins = $db->EscapeString($_POST['coins']);
	
	$db->Query("INSERT INTO `coupons`(code, coins) values('".$code."', '".$coins."')");
	$message = '<div class="message success"><h3>Success!</h3><p>Your coupon code is: '.$code.'</p></div>';
}

$coups = $db->GetNumRows($db->Query("SELECT id FROM `coupons`"));
$active = $db->GetNumRows($db->Query("SELECT id FROM `coupons` WHERE `used`='0'"));
$used = $db->GetNumRows($db->Query("SELECT id FROM `coupons` WHERE `used`='1'"));
?>
            <div id="page-subheader">
                <div class="wrapper clearfix">
                    <nav id="sub-nav">
                        <ul>
                            <li><a href="index.php?x=coupons">Coupons</a></li>
							<li class="active"><a href="index.php?x=addcoupon">Add</a></li>
						</ul>
                    </nav>
                </div>
            </div>
        </header>
      
        <section id="content">
            <div class="wrapper">
                <section class="grid_6 first top">
                    <div class="columns leading">
                        <div class="grid_6 first"><?=$message;?>
						<form id="form" method="post" class="form widget">
                                <header><h2>Add Coupons</h2></header>
                                <section>                 
                                    <fieldset>
                                         <dl>
                                             <dt></dt><dd><label>Coupon Code</label><input type="text" name="code" value="<? echo $code;?>" required="required" /></dd>
											 <dt></dt><dd><label>Coins</label><input type="text" name="coins" value="50" required="required" /></dd>
                                     	</dl>    
                                     </fieldset>
                                     <hr />
                                     <button class="button button-green" type="submit" name="add">Add</button>
                                     <button class="button button-gray" type="reset">Reset</button>
                                </section>
                            </form>
                        </div>
                    </div>


                    <div class="clear">&nbsp;</div>

                </section>

                <aside class="grid_2 top">
                    <div class="accordion">
                        <header class="current"><h2>Coupons</h2></header>
                        <section style="display:block">
                            <dl>
                                <dt>Unused (<?=$active?>)</dt>
                                <dd><div class="progress progress-green"><span style="width: <?= @percent($active, $coups)?>%;"><b><?= @percent($active, $coups)?>%</b></span></div></dd>
								<dt>Used (<?=$used?>)</dt>
                                <dd><div class="progress progress-red"><span style="width: <?= @percent($used, $coups)?>%;"><b><?= @percent($used, $coups)?>%</b></span></div></dd>
                            </dl>
                        </section>
                    </div>
                </aside>
                <div class="clear"></div>

            </div>
            <div id="push"></div>
        </section>
    </div>
    
    <footer id="page-footer">
        <div id="footer-inner">
            <p class="wrapper">All rights reserved &copy; <a href="http://mafianet.org" target="_blank">MafiaNet</a> <?=date('Y')?></p>
        </div>
    </footer>
</body>
</html>